import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {DialogProvider} from "@providers/dialog";
import {StorageProvider} from "@providers/storage";
import {WorkerProvider} from "@providers/interface/worker";

/**
 * Generated class for the WorkerLookDecorationPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-worker-look-decoration',
  templateUrl: 'worker-look-decoration.html',
})
export class WorkerLookDecorationPage {
  worklist: any[] = [];//安装人员列表
  isCheckedWorks = this.navParam.data;//已选中的安装人员列表
  noWork: boolean = false;//不需要安装人员
  userInfo: any;

  constructor(public navParam: NavParams,
              private viewCtrl: ViewController, public navCtrl: NavController,
              private dialog: DialogProvider, private storage: StorageProvider,
              private worker: WorkerProvider) {
    this.userInfo = this.storage.read("userInfo");
  }

  push(page: string) {
    this.navCtrl.push(page);
  }

  ionViewDidLoad() {
    this.getBYRY();
  }

  /**
   * 获取安装人员
   */
  getBYRY() {
    this.dialog.showLoading();
    this.worker.searchFWRY({fwlx: 1})
      .then(message => {
        this.dialog.hideLoading();
        let data = message['data'];
        this.worklist = data.filter(value => {
          return value.LXDH != this.userInfo.MOBILE;
        });
        this.worklist.forEach(value => {
          value.isChecked = false;
          this.isCheckedWorks.forEach(val => {
            if (value.KEY == val.KEY) {
              value.isChecked = true;
            }
          })
        })
      })
      .catch(e => {
        this.dialog.showAlert(e);
        this.dialog.hideLoading();
      });
  }

  dismiss(work?: any) {
    if (work) {
      this.viewCtrl.dismiss(work);
      return;
    }
    this.viewCtrl.dismiss();
  }

  /**
   * 安装人员选中事件
   */
  updateWorks() {
    this.isCheckedWorks = this.worklist.filter(value => {
      return value.isChecked == true;
    });
  }

  updateNoWork() {
    if (this.noWork) {
      this.dismiss([]);
    }
  }


  submit() {
    console.log(this.isCheckedWorks);
    this.dismiss(this.isCheckedWorks);
  }

}
